
package ManagerInfoFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

public class FileInfo {
    private String urlFileName;
    private String tenFile;
    private ListChunk listChunk;
    int kichThuocToiDaChunk = 512*1024;
    private List<byte[]> listData = new ArrayList<byte[]>();
    public FileInfo()
    {
        this.urlFileName ="";
        this.tenFile = "";
        this.listChunk = new ListChunk();
    }
    public void SetURLFileName(String url)
    {
        this.urlFileName = url;
    }
    public void SetTenFile(String fileName)
    {
       this.tenFile = fileName;
    }
    public String GetTenFile()
    {
        return this.tenFile;
    }
    //doc du lieu tua 1 file moi
    public Boolean DocDuLieuTuFile(String urlFileName)
    {  
        try{
            File file = new File(urlFileName);
            byte[] data = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
	    fis.read(data);
            byte[] dataChunk  = new byte[kichThuocToiDaChunk]; 
    
            int phanle=0;
            if(data.length < kichThuocToiDaChunk)
            {
                listData.add(data);
            }
            else
            {
                int n = data.length/kichThuocToiDaChunk;
                phanle = data.length % kichThuocToiDaChunk;

                for(int i = 0;i < n ; i++)
                {
                    dataChunk = new byte[kichThuocToiDaChunk];
                    for(int j = 0; j < kichThuocToiDaChunk; j++)
                    {
                        dataChunk[j] = data[i*kichThuocToiDaChunk + j];
                    }
                    listData.add(dataChunk);
                }
                if(phanle>0)
                {
                    dataChunk = new byte[phanle];
                    for(int i=0; i < phanle;i++)
                    {
                       dataChunk[i] = data[n*kichThuocToiDaChunk+i];
                    }
                    listData.add(dataChunk);
                }
            }
	        
        }
        catch(Exception ex){
            System.out.println("Lỗi Đọc Dữ Liệu Khi Ghi Ra File Chunk"+ex.getMessage());
            return false;
        }
        return true;
    }
    // ghi ra file torrent chứa tên các chunk
    public void GhiFileTorrent(String fileName)
    {
        String[] strfileName = fileName.split("[.]");
        if(strfileName == null)
        {
            System.out.println("Lỗi, tên file null");
            return;
        }
        FileOutputStream fos,fosChunk;
        String str;
        try {
            fos = new FileOutputStream(new File("./Data/"+strfileName[0]+".torrent"));
            
            str = fileName+"\n";
            fos.write(str.getBytes());
            for(int i=0; i < listData.size();i++)
            {   
          	//ghi ra file chunk
            	fosChunk = new FileOutputStream(new File("./Data/Share/"+SHA1(listData.get(i))+".chunk"));
            	fosChunk.write(listData.get(i));
            	System.out.println(SHA1(listData.get(i)));
            	fosChunk.flush();
            	fosChunk.close();
            	
            	//ghi ra thong tin file 
            	str = SHA1(listData.get(i))+"\n";
            	fos.write(str.getBytes());
            }
            fos.flush();
            fos.close();
        }catch (FileNotFoundException ex) {
            System.out.println("Lỗi, Không Tìm Thấy Đường Dẫn"+ ex.getMessage());
        }catch(IOException io) {
            System.out.println("Lỗi ghi file "+ io.getMessage());
        }

    }
    // ghi ra chuoi ma hoa SHA1 theo dung luong file
    public String SHA1(byte[] data)
    {
        StringBuffer sb = new StringBuffer();
        try
        {
            MessageDigest mDigest = MessageDigest.getInstance("SHA1");
            byte[] result = mDigest.digest(data);
            for(int i=0; i<result.length;i++)
            {
                sb.append(Integer.toString((result[i] & 0xff) + 0x100, 16).substring(1));
            }
        }
        catch (NoSuchAlgorithmException ex)
        {
                ex.printStackTrace();
        }
        return sb.toString();
    }
}
